package cac.gkinterface.service;

import cac.common.CallInterFace.service.CallInterfaceService;
import cac.gkinterface.dao.WorkPackageDao;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import net.logstash.logback.encoder.org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ErpBackAsync {
    @Autowired
    private WorkPackageDao workPackageDao;
    @Autowired
    private CallInterfaceService callInterfaceService;
    @Autowired
    private WorkService workService;

    public void resultBackToWmsAsync(JSONArray paramJsonObjectArray, String msgId, String func){
        ErpBackAsync erpBackAsyncProxy = SpringUtil.getBean(ErpBackAsync.class);
        erpBackAsyncProxy.resultBackToWms(paramJsonObjectArray,msgId,func);
    }
    public void resultBackToErpAsync(JSONArray paramJsonObjectArray, String msgId){
        ErpBackAsync erpBackAsyncProxy = SpringUtil.getBean(ErpBackAsync.class);
        erpBackAsyncProxy.resultBackToErp(paramJsonObjectArray,msgId);
    }
    //ERP返回处理结果到wms
    @Async
    public void resultBackToWms(JSONArray paramJsonObjectArray, String msgId, String func) {
        List<HashMap> list = JSONArray.parseArray(paramJsonObjectArray.toJSONString(),HashMap.class);
        for(HashMap map : list){
            map.put("func", func);
            map.put("BData", map.toString());
            map.put("parentMsgId", msgId);
            map.put("system", "ERP");
            log.info("ERP查询101处理结果接口参数:" + map);
            //插入信息日志：ERP-101(记录消息id)
            this.issuedBack(map);
        }
    }
    //ERP主动查询wms处理结果,WMS返回处理结果
    @Async
    public void resultBackToErp(JSONArray paramJsonObjectArray, String msgId) {
        HashMap backToErp = new HashMap();
        List datas = new ArrayList();
        HashMap issuedTest = new HashMap();
        String rstl="";
        try {
            //多个消息id
            // List<HashMap> list = paramJsonObjectArray.toJavaList(HashMap.class);
            List<HashMap> list = JSONArray.parseArray(paramJsonObjectArray.toJSONString(),HashMap.class);
            for(HashMap map : list){
                List<HashMap> rsl = workPackageDao.searchWmsResult(map);
                for (HashMap data : rsl) {
                    if (Objects.isNull(data.get("failMsg"))) {
                        data.put("failMsg", "");
                    }
                    data.put("system", "MSMS");
                    datas.add(data);
                }
                if(rsl.size()==0){
                    HashMap emptyData = new HashMap();
                    emptyData.put("retCode","0");
                    emptyData.put("system","MSMS");
                    emptyData.put("msgId",map.get("msgId"));
                    emptyData.put("failMsg","未找到数据");
                    datas.add(emptyData);
                }
            }
            backToErp.put("data", datas);
            backToErp.put("msgId", msgId);
            backToErp.put("func", "101-ERP-RCVMSG");
            log.info("erp主动查询结果返回！");
            callInterfaceService.sendBData("sendBData", "两网传输-发送业务数据",
                    CallInterfaceService.encodeBData(JSON.toJSONString(backToErp)), "反馈ERP查询结果");
            //接口日志参数
            if(datas.toString().length()>3900){
                issuedTest.put("BData", datas.toString().substring(0,3900));
            }else{
                issuedTest.put("BData", datas.toString());
            }
            issuedTest.put("type", "101-ERP-RCVMSG");
            issuedTest.put("msgId", msgId);
            issuedTest.put("retCode", "1");
            rstl = "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
            rstl = ExceptionUtils.getFullStackTrace(e);
            if(rstl.length()>200){
                rstl = rstl.substring(0, 200);
            }
            issuedTest.put("result", rstl);
            issuedTest.put("retCode", "0");
        } finally {
            issuedTest.put("failMsg", rstl);
            workService.issuedTest(issuedTest);
        }
    }
    //erp返回消息结果记录
    public Integer issuedBack(HashMap hashMap) {
        return workPackageDao.issuedBack(hashMap);
    }
}
